package com.yanqu.road.dao.impl.channelcenter.entername;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.entername.UserEnterName;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserEnterNameDaoImpl extends TempDataObjectDao<UserEnterName> {


    public static String getUserEnterNameSql = "select * from `t_n_activity_enter_name` where `activityId`=? ";
    public Map<Long, UserEnterName> getUserEnterName(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UserEnterName> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getChannelServerDb());
            call = getDb().getConnection().prepareCall(getUserEnterNameSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserEnterName date = parse(rs);
                result.put(date.getUserId(), date);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateUserEnterNameSql = "update `t_n_activity_enter_name` set zoneId=?,serverId=?,channelId=?,groupId=?,getDisperseGold=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserEnterName userEnterName) {
        userEnterName.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userEnterName.getZoneId()));
        para.put(i++, new DbParameter(Types.BIGINT, userEnterName.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, userEnterName.getChannelId()));
        para.put(i++, new DbParameter(Types.INTEGER, userEnterName.getGroupId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userEnterName.isGetDisperseGold()));
        para.put(i++, new DbParameter(Types.INTEGER, userEnterName.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userEnterName.getUserId()));
        if (getDb().executeNoneQuery(updateUserEnterNameSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserEnterNameSql = "insert into `t_n_activity_enter_name`(activityId,userId,zoneId,serverId,channelId,groupId,getDisperseGold) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserEnterName userEnterName) {
        userEnterName.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userEnterName.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userEnterName.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userEnterName.getZoneId()));
        para.put(i++, new DbParameter(Types.BIGINT, userEnterName.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, userEnterName.getChannelId()));
        para.put(i++, new DbParameter(Types.INTEGER, userEnterName.getGroupId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userEnterName.isGetDisperseGold()));
        if (getDb().executeNoneQuery(insertUserEnterNameSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserEnterName parseData(ResultSet rs) throws SQLException {
        UserEnterName userEnterName = new UserEnterName();
        userEnterName.setActivityId(rs.getInt("activityId"));
        userEnterName.setUserId(rs.getLong("userId"));
        userEnterName.setZoneId(rs.getInt("zoneId"));
        userEnterName.setServerId(rs.getLong("serverId"));
        userEnterName.setChannelId(rs.getInt("channelId"));
        userEnterName.setGroupId(rs.getInt("groupId"));
        userEnterName.setGetDisperseGold(rs.getBoolean("getDisperseGold"));
        return userEnterName;
    }
}
